import HomePage from './pages/home.vue';
import AboutPage from './pages/about.vue';
import FormPage from './pages/form.vue';

import TermsPopup from './popups/terms.vue';

const configFolder = require('../build/config-folder.js');


var indexRoute = '/home/';

var isIndex = false;

// if index, redirect to '/' first to play page in animation
function handleIndex(routeTo, reject) {

  if (!isIndex) {

    isIndex = true;
    reject();
    
    var $f7 = document.getElementById("framework7-root").__vue__.$f7;
    indexRoute = routeTo.path;
    $f7.router.navigate('/');

    return false;
  }
  else {
    return true;
  }

}



export default [

  {
    path: (configFolder.projectFolder == '') ? '/' : '/'+configFolder.projectFolder+'/', // pushState : true
    alias: '/', // pushState : false
    
    beforeEnter: function (routeTo, routeFrom, resolve, reject) {

      // must use beforeEnter for entry point redirect, 
      // to fix Safari history bug and add page in animation
      isIndex = true;
      reject();
      
      var $f7 = document.getElementById("framework7-root").__vue__.$f7;
      var urlQuery = $f7.utils.parseUrlQuery(document.location.href);

      var route = (urlQuery.route && urlQuery.route != '') ? urlQuery.route : indexRoute;

      if (routeFrom && routeFrom.path == route) {
        $f7.router.navigate(route, {'reloadCurrent' : true});
        window.location.reload();
      }
      else {
        $f7.router.navigate(route);
      }
      
    }
    
  },

  // page
  {
    path: '/home/',
    component: HomePage,
    beforeEnter: function (routeTo, routeFrom, resolve, reject) {
      if (!handleIndex(routeTo, reject)) return;
      
      // custom code
      resolve();
      
    }
  },
  {
    path: '/about/',
    component: AboutPage,
    beforeEnter: function (routeTo, routeFrom, resolve, reject) {
      if (!handleIndex(routeTo, reject)) return;
      
      // custom code
      resolve();
      
    }
  },
  {
    path: '/form/',
    component: FormPage,
    beforeEnter: function (routeTo, routeFrom, resolve, reject) {
      if (!handleIndex(routeTo, reject)) return;
      
      // custom code
      resolve();
      
    },
  },

  // popup
  {
    path: '/terms/',
    popup: {
      component: TermsPopup,
    }
  },



];
